package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.aijk.ehm.dao.mybatis.EhmCompanyDeptMapper;
import com.aijk.ehm.model.EhmCompanyDept;
import com.aijk.ehm.model.EhmCompanyDeptExample;
import com.aijk.ehm.model.qo.EhmCompanyDeptQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EhmCompanyDeptService {
    private static Logger logger = LoggerFactory.getLogger(EhmCompanyDeptService.class);

    @Autowired
    private EhmCompanyDeptMapper mapper;

    public EhmCompanyDeptExample getExample(EhmCompanyDeptQo qo) {
        EhmCompanyDeptExample example = new EhmCompanyDeptExample();
        EhmCompanyDeptExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (Utils.isNotEmpty(qo.getStatus())) criteria.andStatusEqualTo(qo.getStatus());
            if (qo.getTitle()!=null && !qo.getTitle().equals("")) criteria.andTitleLike("%"+qo.getTitle()+"%");
            if (qo.getSn()!=null && !qo.getSn().equals("")) criteria.andSnEqualTo(qo.getSn());
            if (qo.getCompanyId()!=null && qo.getCompanyId()!=0) criteria.andCompanyIdEqualTo(qo.getCompanyId());

        }
        return example;
    }

    public int count(EhmCompanyDeptQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(EhmCompanyDept bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int update(EhmCompanyDept bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public EhmCompanyDept findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<EhmCompanyDept> find(EhmCompanyDeptQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<EhmCompanyDept> page(PagingRequest pagingRequest, EhmCompanyDeptQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<EhmCompanyDept>(mapper.selectByExample(getExample(qo)));
    }
}